package com.example.demo.model.order.order.service.impl;

import com.example.demo.Results.ResultClass;
import com.example.demo.model.goods.goods.entity.ShopGoods;
import com.example.demo.model.goods.goods.mapper.ShopGoodsDao;
import com.example.demo.model.index.index.entity.ShopCartReq;
import com.example.demo.model.order.order.entity.UserOrder;
import com.example.demo.model.order.order.entity.UserOrderDetails;
import com.example.demo.model.order.order.entity.request.FinishResp;
import com.example.demo.model.order.order.mapper.UserOrderDao;
import com.example.demo.model.order.order.mapper.UserOrderDetailsDao;
import com.example.demo.model.order.order.service.UserOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.model.user.user.entity.ShopUser;
import com.example.demo.model.user.user.entity.UserAddress;
import com.example.demo.model.user.user.mapper.ShopUserDao;
import com.example.demo.model.user.user.mapper.UserAddressDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author STAHYW
 * @since 2021-10-23
 */
@Service
public class UserOrderServiceImpl extends ServiceImpl<UserOrderDao, UserOrder> implements UserOrderService {
    @Autowired
    ShopGoodsDao shopGoodsDao;
    @Autowired
    UserOrderDao userOrderDao;
    @Autowired
    ShopUserDao shopUserDao;
    @Autowired
    UserOrderDetailsDao userOrderDetailsDao;
    @Autowired
    UserAddressDao userAddressDao;


    @Transactional
    @Override
    public ResultClass goMoney(List<ShopCartReq> shopCartReqs, Integer addressId, HttpServletRequest request) {

        Integer totalMoney = 0;
        Integer userId = Integer.valueOf(request.getSession().getAttribute("userId").toString());
        ShopUser shopUser = shopUserDao.selectById(userId);
        Integer userMoney = shopUser.getUserMoney();

        for (ShopCartReq shopCartReq : shopCartReqs) {
            ShopGoods shopGoods = shopGoodsDao.selectById(shopCartReq.getId());
            if (shopGoods.getShopGoodsCount() < shopCartReq.getCount()) {
                return ResultClass.fail("对不起，该商品已卖光");
            }
            //计算总金额
            int itemMoney = shopGoods.getShopGoodsPrice() * shopCartReq.getCount();
            totalMoney += itemMoney;
        }
        if (userMoney < totalMoney) {
            return ResultClass.fail("余额不足");
        }

        //插入订单表
        UserOrder order = new UserOrder();
        order.setTotalMoney(totalMoney);
        order.setSubTime(LocalDateTime.now());
        order.setUserId(userId);
        order.setOrderStatus(1);
        order.setAddressId(addressId);
        String uuid = UUID.randomUUID().toString();
        order.setOrderNumber(uuid);
        userOrderDao.insert(order);



        //修改用户余额
        shopUser.setUserMoney(userMoney-totalMoney);
        shopUserDao.updateById(shopUser);


        for (ShopCartReq shopCartReq : shopCartReqs) {
            //修改商品数量
            ShopGoods shopGoods = shopGoodsDao.selectById(shopCartReq.getId());
            shopGoods.setShopGoodsCount(shopGoods.getShopGoodsCount() - shopCartReq.getCount());
            shopGoodsDao.updateById(shopGoods);

            //插入流水表
            UserOrderDetails userOrderDetails = new UserOrderDetails();
            userOrderDetails.setUserOrderId(order.getId());
            userOrderDetails.setGoodsCount(shopCartReq.getCount());
            userOrderDetails.setGoodsId(shopCartReq.getId());
            userOrderDetails.setSubTime(LocalDateTime.now());
            userOrderDetailsDao.insert(userOrderDetails);
        }

        return ResultClass.ok(order.getId());
    }

    @Override
    public ModelAndView finish(Integer orderId) {
        UserOrder order = userOrderDao.selectById(orderId);
        UserAddress useAddress = userAddressDao.selectById(order.getAddressId());

        List<FinishResp> list = userOrderDetailsDao.getOrder( orderId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/order/finish");
        modelAndView.addObject("useAddress",useAddress);
        modelAndView.addObject("list",list);
        modelAndView.addObject("order",order);

        return modelAndView;
    }
}
